<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: LogViewer.php 468 2011-01-25 17:25:12Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalLogRecord');

class LogViewer extends PortalAdminPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('Log Viewer');

		if(!$this->IsPostBack)
		{
			$this->bindData();
		}
	}

	public function getData($offset, $rows)
	{
		$criteria			= new TActiveRecordCriteria();
		$criteria->Limit	= $rows;
		$criteria->Offset	= $offset;
		$criteria->OrdersBy['logtime']='asc';
		
		return PortalLogRecord::finder()->findAll($criteria);
	}

	public function bindData()
	{
		$this->datagridLog->DataSource			= $this->getData(0, $this->datagridLog->PageSize);
		$this->datagridLog->VirtualItemCount	= $this->RowCount;
		$this->datagridLog->dataBind();
	}

	protected function getRowCount()
    {
		return PortalLogRecord::finder()->count();
    }

	public function pagerCreated($sender,$param)
	{
		$param->Pager->Controls->insertAt(0, Prado::localize('Page').': ');
	}

	public function itemCreated($sender,$param)
	{
		$item=$param->Item;
		if($item->ItemType==='Item' || $item->ItemType==='AlternatingItem' || $item->ItemType==='EditItem')
		{
			// add an aleart dialog to delete buttons
			$item->DeleteColumn->Button->Attributes->onclick = "if(!confirm('" . Prado::localize('Are you sure?')."')) return false;";
		}
	}

	public function deleteItem($sender,$param)
	{
		$item = $param->Item;
		$Id = $this->datagridLog->DataKeys[$item->ItemIndex];
		PortalLogRecord::finder()->deleteByid($Id);
		$this->gotoPage('Admin.LogViewer');
	}

	public function sortDataGrid($sender,$param)
	{
		$this->datagridLog->DataSource=$this->sortData($this->getData(0,$this->datagridLog->PageSize),$param->SortExpression);
		$this->datagridLog->dataBind();
	}

	protected function sortData($data,$key)
	{
		$compare = create_function('$a,$b','if ($a["'.$key.'"] == $b["'.$key.'"]) {return 0;}else {return ($a["'.$key.'"] > $b["'.$key.'"]) ? 1 : -1;}');
		usort($data,$compare) ;
		return $data ;
	}

	public function ClearLog($s,$p)
	{
		$this->DataAccess->createCommand("delete from tbllog")->execute();
		$this->bindData();
	}

	public function changePage($sender, $param)
	{
		$this->datagridLog->CurrentPageIndex	= $param->NewPageIndex;
        $offset									= $param->NewPageIndex*$this->datagridLog->PageSize;
        $this->datagridLog->DataSource			= $this->getData($offset,$this->datagridLog->PageSize);
        $this->datagridLog->dataBind();
	}
}
?>